package com.dykj.fireFighting.web.controller.GB26875Controller;

import com.alibaba.fastjson.JSON;
import com.dykj.fireFighting.web.contact.PubContacts;
import com.dykj.fireFighting.web.model.Device;
import com.dykj.fireFighting.web.model.report.DevHisNbEdge;
import com.dykj.fireFighting.web.model.report.DevHisNbExtinguisher;
import com.dykj.fireFighting.web.model.report.DevHisNbHydrant;
import com.dykj.fireFighting.web.model.report.DevHisNbWater;
import com.dykj.fireFighting.web.service.*;
import com.dykj.fireFighting.web.utils.JsonKeyEdit;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "/chinaTelecom")
public class ChinaTelecomControllrt {
    @Autowired
    private DevHisNbHydrantService devHisNbHydrantService;
    @Autowired
    private DevHisNbExtinguisherService devHisNbExtinguisherService;
    @Autowired
    private DevHisNbEdgeService devHisNbEdgeService;
    @Autowired
    private DevHisNbWaterService devHisNbWaterService;
    @Autowired
    private ManufactorService manufactorService;
    @ResponseBody
    @RequestMapping(value = "/client", method = {RequestMethod.POST})
    public void client(@RequestBody Map<String,Object> map){
        /*String  messageType=map.get("messageType").toString();//消息类型   设备上下线：deviceOnlineOfflineReport；时间上报：eventReport；设备数据变化：dataReport
        if("deviceOnlineOfflineReport".equals(messageType)){

        }*/
        try {
            SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("---------------------"+sdf.format(new Date())+"---接收数据-----------------------");
            Set keSet=map.entrySet();
            for(Iterator itr = keSet.iterator(); itr.hasNext();){
                Map.Entry me=(Map.Entry)itr.next();
                Object ok=me.getKey(); //获取参数名`
                Object ov=me.getValue(); //获取参数值
                String[] value=new String[1];
                if(ov instanceof String[]){
                    value=(String[])ov;
                }else{
                    value[0]=ov.toString();
                }
                for(int k=0;k <value.length;k++){
                    System.out.println(ok+"="+value[k]);
                }
            }
            System.out.println("--------------------------------------------------------------------------");


            Object deviceId=map.get("deviceId");
            if(null == deviceId){
                System.out.println("----------设备Id空----------");
                return;
            }
            String deviceIdStr=deviceId.toString();
            Device device=manufactorService.selectDeviceById(deviceIdStr);
            if(null == device){
                System.out.println("----------设备不存在----------");
                return;
            }
            if(device.getDeviceType() != null && PubContacts.HYDRANT.equals(device.getDeviceType())){ //  消防栓设备
                Object eventContentObj=map.get("eventContent");
                Object payloadObj=map.get("payload");
                DevHisNbHydrant devHisNbHydrant=JSON.parseObject(JSON.toJSONString(map), DevHisNbHydrant.class);
                DevHisNbHydrant devHisNbHydrant1=new DevHisNbHydrant();
                if(null != eventContentObj){
                    String eventContent=JSON.toJSONString(eventContentObj);//.toString();
                    eventContent= JsonKeyEdit.camelCase(eventContent);
                    JSONObject obj = new JSONObject().fromObject(eventContent);
                    devHisNbHydrant1 = (DevHisNbHydrant)JSONObject.toBean(obj,DevHisNbHydrant.class);
                }
                if(null != payloadObj){
                    String payload=JSON.toJSONString(payloadObj);//.toString();
                    payload= JsonKeyEdit.camelCase(payload);
                    JSONObject obj = new JSONObject().fromObject(payload);
                    devHisNbHydrant1 =(DevHisNbHydrant)JSONObject.toBean(obj,DevHisNbHydrant.class);
                }
                DevHisNbHydrant devHisNbHydrantInsert=combineSydwCore(devHisNbHydrant,devHisNbHydrant1);
                devHisNbHydrantService.insert(devHisNbHydrantInsert);
                System.out.println(devHisNbHydrantInsert);
            }else if(device.getDeviceType() != null && PubContacts.EXTINGUISHER.equals(device.getDeviceType())){ //  灭火器设备
                Object eventContentObj=map.get("eventContent");
                Object payloadObj=map.get("payload");
                DevHisNbExtinguisher devHisNbExtinguisher=JSON.parseObject(JSON.toJSONString(map), DevHisNbExtinguisher.class);
                DevHisNbExtinguisher devHisNbExtinguisher1=new DevHisNbExtinguisher();
                if(null != eventContentObj){
                    String eventContent=JSON.toJSONString(eventContentObj);//.toString();
                    eventContent= JsonKeyEdit.camelCase(eventContent);
                    JSONObject obj = new JSONObject().fromObject(eventContent);
                    devHisNbExtinguisher1 = (DevHisNbExtinguisher)JSONObject.toBean(obj,DevHisNbExtinguisher.class);
                }
                if(null != payloadObj){
                    String payload=JSON.toJSONString(payloadObj);//.toString();
                    payload= JsonKeyEdit.camelCase(payload);
                    JSONObject obj = new JSONObject().fromObject(payload);
                    devHisNbExtinguisher1 =(DevHisNbExtinguisher)JSONObject.toBean(obj,DevHisNbExtinguisher.class);

                }
                DevHisNbExtinguisher devHisNbExtinguisherInsert=(DevHisNbExtinguisher)combineSydwCoreObject(devHisNbExtinguisher,devHisNbExtinguisher1);
                devHisNbExtinguisherService.insert(devHisNbExtinguisherInsert);
                System.out.println(devHisNbExtinguisherInsert);
            }else if(device.getDeviceType() != null && PubContacts.EDGE.equals(device.getDeviceType())){ //  边缘设备
                Object eventContentObj=map.get("eventContent");
                Object payloadObj=map.get("payload");
                DevHisNbEdge devHisNbEdge=JSON.parseObject(JSON.toJSONString(map), DevHisNbEdge.class);
                DevHisNbEdge devHisNbEdge1=new DevHisNbEdge();
                if(null != eventContentObj){
                    String eventContent=JSON.toJSONString(eventContentObj);//.toString();
                    eventContent= JsonKeyEdit.camelCase(eventContent);
                    JSONObject obj = new JSONObject().fromObject(eventContent);
                    devHisNbEdge1 = (DevHisNbEdge)JSONObject.toBean(obj,DevHisNbEdge.class);
                }
                if(null != payloadObj){
                    String payload=JSON.toJSONString(payloadObj);//.toString();
                    payload= JsonKeyEdit.camelCase(payload);
                    JSONObject obj = new JSONObject().fromObject(payload);
                    devHisNbEdge1 =(DevHisNbEdge)JSONObject.toBean(obj,DevHisNbEdge.class);

                }
                DevHisNbEdge devHisNbEdgeInsert=(DevHisNbEdge)combineSydwCoreObject(devHisNbEdge,devHisNbEdge1);
                devHisNbEdgeService.insert(devHisNbEdgeInsert);
                System.out.println(devHisNbEdgeInsert);
            }else if(device.getDeviceType() != null && PubContacts.WATER.equals(device.getDeviceType())){ //  水位设备
                Object eventContentObj=map.get("eventContent");
                Object payloadObj=map.get("payload");
                DevHisNbWater devHisNbWater=JSON.parseObject(JSON.toJSONString(map), DevHisNbWater.class);
                DevHisNbWater devHisNbWater1=new DevHisNbWater();
                if(null != eventContentObj){
                    String eventContent=JSON.toJSONString(eventContentObj);
                    eventContent= JsonKeyEdit.camelCase(eventContent);
                    JSONObject obj = new JSONObject().fromObject(eventContent);
                    devHisNbWater1 = (DevHisNbWater)JSONObject.toBean(obj,DevHisNbWater.class);
                }
                if(null != payloadObj){
                    String payload=JSON.toJSONString(payloadObj);//.toString();
                    payload= JsonKeyEdit.camelCase(payload);
                    JSONObject obj = new JSONObject().fromObject(payload);
                    devHisNbWater1 =(DevHisNbWater)JSONObject.toBean(obj,DevHisNbWater.class);
                }
                DevHisNbWater devHisNbWaterInsert=(DevHisNbWater)combineSydwCoreObject(devHisNbWater,devHisNbWater1);
                devHisNbWaterService.insert(devHisNbWaterInsert);
                System.out.println(devHisNbWaterInsert);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /*
    以第一个实体类为主，如果第一个的实体类某个字段为空，则会吧第二个实体类的值取过来进行赋值，
    如果不为空的则不作改变
     */
    private static DevHisNbHydrant combineSydwCore(DevHisNbHydrant sourceBean, DevHisNbHydrant targetBean) {
        Class sourceBeanClass = sourceBean.getClass();
        Class targetBeanClass = targetBean.getClass();

        Field[] sourceFields = sourceBeanClass.getDeclaredFields();
        Field[] targetFields = targetBeanClass.getDeclaredFields();
        for (int i = 0; i < sourceFields.length; i++) {
            Field sourceField = sourceFields[i];
            if (Modifier.isStatic(sourceField.getModifiers())) {
                continue;
            }
            Field targetField = targetFields[i];
            if (Modifier.isStatic(targetField.getModifiers())) {
                continue;
            }
            sourceField.setAccessible(true);
            targetField.setAccessible(true);
            try {
                if (!(sourceField.get(sourceBean) == null) && !"serialVersionUID".equals(sourceField.getName().toString())) {
                    targetField.set(targetBean, sourceField.get(sourceBean));
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return targetBean;
    }

    //针对所用对象
    private static Object combineSydwCoreObject(Object sourceBean, Object targetBean) {
        Class sourceBeanClass = sourceBean.getClass();
        Class targetBeanClass = targetBean.getClass();

        Field[] sourceFields = sourceBeanClass.getDeclaredFields();
        Field[] targetFields = sourceBeanClass.getDeclaredFields();
            for (int i = 0; i < sourceFields.length; i++) {
            Field sourceField = sourceFields[i];
            Field targetField = targetFields[i];
            sourceField.setAccessible(true);
            targetField.setAccessible(true);
            try {
                if (!(sourceField.get(sourceBean) == null)) {
                    targetField.set(targetBean, sourceField.get(sourceBean));
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return targetBean;
    }

}
